﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class BangChietTinh : AdminPageBase
    {
        protected decimal TotalPriceCalculators = 0;
        protected decimal TotalPriceCalculatorAdults = 0;
        protected decimal TotalPriceCalculatorChilds = 0;

        protected List<CalculatorInfo> TicketAirs = new List<CalculatorInfo>();
        protected decimal TotalTicketAirPrices = 0;
        protected decimal TotalTicketAirPricesAdults = 0;
        protected decimal TotalTicketAirPricesChild = 0;

        protected List<CalculatorInfo> TicketCars = new List<CalculatorInfo>();
        protected decimal TotalTicketCarPrices = 0;
        protected decimal TotalTicketCarPricesAdults = 0;
        protected decimal TotalTicketCarPricesChild = 0;

        protected List<CalculatorInfo> TicketStations = new List<CalculatorInfo>();
        protected decimal TotalTicketStationPrices = 0;
        protected decimal TotalTicketStationPricesAdults = 0;
        protected decimal TotalTicketStationPricesChild = 0;

        protected List<CalculatorInfo> tourMenu = new List<CalculatorInfo>();
        protected List<CalculatorInfo> tourVisa = new List<CalculatorInfo>();
        public CalculatorInfo tourDVK = new CalculatorInfo();
        protected decimal TotalMenuPrices = 0;
        protected decimal TotalMenuAdults = 0;
        protected decimal TotalMenuChild = 0;

        protected decimal TotalVisaPrices = 0;
        protected decimal TotalVisaAdults = 0;
        protected decimal TotalVisaChild = 0;

        protected decimal TotalDVKPrices = 0;
        protected decimal TotalDVKAdults = 0;
        protected decimal TotalDVKChild = 0;

        protected decimal TotalHHPrices = 0;
        protected decimal TotalHHAdults = 0;
        protected decimal TotalHHChild = 0;

        protected List<CalculatorInfo> InsuranceList = new List<CalculatorInfo>();
        protected decimal TotalInsurancePrices = 0;
        protected decimal TotalInsurancePricesAdults = 0;
        protected decimal TotalInsurancePricesChild = 0;

        protected List<CalculatorInfo> TravelGuideCashs = new List<CalculatorInfo>();
        protected decimal TotalTravelGuidePrices = 0;

        protected List<CalculatorInfo> TicketHotel = new List<CalculatorInfo>();
        protected decimal TotalTicketHotelPrices = 0;
        protected decimal TotalTicketHotelPricesAdults = 0;
        protected decimal TotalTicketHotelPricesChild = 0;

        private List<CalculatorInfo> _calculatorList = new List<CalculatorInfo>();
        private readonly CalculatorController _calculatorController = new CalculatorController();
        protected TourInfo tourInfo = new TourInfo();
        public List<CalculatorInfo> _calculatorListDvk = new List<CalculatorInfo>();
        public List<CalculatorInfo> _calculatorListHH = new List<CalculatorInfo>();
        private readonly CalculatorController _calculatorControllerDvk = new CalculatorController();
        private TourInfo _tourInfo = new TourInfo();
        private  readonly TourController _tourController = new TourController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                _calculatorList = _calculatorController.ListCalculator().Where(p=>p.TourId==int.Parse(Request.QueryString["TourId"])).ToList();
                tourInfo = new TourController().GetTour(int.Parse(Request.QueryString["TourId"]));
                _calculatorListDvk =
                   _calculatorControllerDvk.ListCalculatorDvk()
                       .Where(p => p.TourId == int.Parse(Request.QueryString["TourId"]) && p.ItemType == 8).ToList();
                _calculatorListHH =
                  _calculatorControllerDvk.ListCalculatorDvk()
                      .Where(p => p.TourId == int.Parse(Request.QueryString["TourId"]) && p.ItemType == 11).ToList();
                _tourInfo = tourInfo;

                LoadFlightArrivals();
                LoadSchedule();
                LoadScheduleStation();
                LoadConcern();
                LoadConcernStation();
                LoadScheduleCar();
                LoadSoChoCar();
                LoadConcernCar();
                LoadYearCar();
                
                LoadTime();
                

                LoadTime();
                RenderForm();
            }
            //if (_calculatorList.Count == 0) return;
            
            
        }
        private void RenderForm()
        {

            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                //txtQuantityAdultsTour.Text = _tourInfo.QuantityAdult.ToString();
                //txtQuantityChildTour.Text = _tourInfo.QuantityChild.ToString();

                //dteBeginDateTour.Value = _tourInfo.BeginDate.ToString("dd/MM/yyyy");

                //ddlBeginHourTour.SelectedValue = _tourInfo.BeginDate.Hour.ToString();
                //ddlBeginMinuteTour.SelectedValue = _tourInfo.BeginDate.Minute.ToString();
                //dteEndDateTour.Value = _tourInfo.EndDate.ToString("dd/MM/yyyy");

                //ddlEndHourTour.SelectedValue = _tourInfo.EndDate.Hour.ToString();
                //ddlEndMinuteTour.SelectedValue = _tourInfo.EndDate.Minute.ToString();
                //txtTotalDayTour.Text = _tourInfo.QuantityDay.ToString();

                //txtTotalNightTour.Text = _tourInfo.QuantityNight.ToString();

                lbQuantityAdultsTour.Text = tourInfo.QuantityAdult.ToString();
                lbQuantityChildTour.Text = tourInfo.QuantityChild.ToString();
                lbBeginDateTour.Text = tourInfo.BeginDate.ToString("dd/MM/yyyy");
                lbEndDateTour.Text = tourInfo.EndDate.ToString("dd/MM/yyyy");
                lbTotalDayTour.Text = tourInfo.QuantityDay.ToString();
                lbTotalNightTour.Text = tourInfo.QuantityNight.ToString();

                TicketAirs = _calculatorList.Where(p => p.ItemType == 1).ToList();
                TotalTicketAirPricesAdults = TicketAirs.Sum(p => p.TotalPriceAirAdults);
                TotalTicketAirPricesChild = TicketAirs.Sum(p => p.TotalPriceAirChild);
                TotalTicketAirPrices = TotalTicketAirPricesAdults + TotalTicketAirPricesChild;


                TicketStations = _calculatorList.Where(p => p.ItemType == 2).ToList();
                TotalTicketStationPricesAdults = TicketStations.Sum(p => p.TotalPriceStationAdults);
                TotalTicketStationPricesChild = TicketStations.Sum(p => p.TotalPriceStationChild);
                TotalTicketStationPrices = TotalTicketStationPricesAdults + TotalTicketStationPricesChild;

                TicketCars = _calculatorList.Where(p => p.ItemType == 3).ToList();
                TotalTicketCarPrices = TicketCars.Sum(p => p.TotalPriceCar);
                var priceCarOnCustomer = TotalTicketCarPrices/(tourInfo.QuantityAdult + tourInfo.QuantityChild);
                TotalTicketCarPricesAdults = priceCarOnCustomer*tourInfo.QuantityAdult;
                TotalTicketCarPricesChild = priceCarOnCustomer*tourInfo.QuantityChild;

                //Menu
                tourMenu = _calculatorList.Where(p => p.ItemType == 4).ToList();
                TotalMenuAdults = tourMenu.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
                TotalMenuChild = tourMenu.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
                TotalMenuPrices = TotalMenuChild + TotalMenuAdults;
                //Visa
                tourVisa = _calculatorList.Where(p => p.ItemType == 6).ToList();
                TotalVisaChild = tourVisa.Sum(p => p.TotalPriceVisaChild);
                TotalVisaAdults = tourVisa.Sum(p => p.TotalPriceVisaAdults);
                TotalVisaPrices = TotalVisaChild + TotalVisaAdults;
                //hoa hoong 
                TotalHHAdults = _calculatorListHH.Sum(p => p.TotalPriceDvkAdults);
                TotalHHChild = _calculatorListHH.Sum(p => p.TotalPriceDvkChild);
                TotalHHPrices = TotalHHAdults + TotalHHChild; 

                //hdv
                TravelGuideCashs = _calculatorList.Where(p => p.ItemType == 9).ToList();
                TotalTravelGuidePrices = TravelGuideCashs.Sum(p => p.TotalPriceTravelGuide);
                //ks
                TicketHotel = _calculatorList.Where(p => p.ItemType == 5).ToList();
                TotalTicketHotelPricesAdults = TicketHotel.Sum(p => p.TotalPriceHotelAdults);
                TotalTicketHotelPricesChild = TicketHotel.Sum(p => p.TotalPriceHotelChild);
                TotalTicketHotelPrices = TotalTicketHotelPricesAdults + TotalTicketHotelPricesChild;
                //bh
                InsuranceList = _calculatorList.Where(p => p.ItemType == 7).ToList();
                TotalInsurancePrices = InsuranceList.Sum(p => p.TotalPriceInsuranceAdults);
                TotalInsurancePricesAdults = InsuranceList.Sum(p => p.TotalPriceInsuranceChild);
                TotalInsurancePricesChild = TotalInsurancePrices + TotalInsurancePricesAdults;


                


            }
        }
        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
            //RedirectLink();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            //RedirectLink();
        }
        //protected void SaveData()
        //{
        //    //_tourInfo = _tourController.GetTour(int.Parse(Request.QueryString["TourId"]));


        //    //_tourInfo.QuantityAdult = int.Parse(txtQuantityAdultsTour.Text);
        //    //_tourInfo.QuantityChild = int.Parse(txtQuantityChildTour.Text);
        //    //if (!String.IsNullOrEmpty(dteBeginDateTour.Value))
        //    //{
        //    //    var beginDate = Convert.ToDateTime(dteBeginDateTour.Value);
        //    //    _tourInfo.BeginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, int.Parse(ddlBeginHourTour.SelectedValue), int.Parse(ddlBeginMinuteTour.SelectedValue), 0);
        //    //}
        //    //if (!String.IsNullOrEmpty(dteEndDateTour.Value))
        //    //{
        //    //    var endDate = Convert.ToDateTime(dteEndDateTour.Value);
        //    //    _tourInfo.EndDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, int.Parse(ddlEndHourTour.SelectedValue), int.Parse(ddlEndMinuteTour.SelectedValue), 0);
        //    //}
        //    //_tourInfo.QuantityDay = int.Parse(txtTotalDayTour.Text);
        //    //_tourInfo.QuantityNight = int.Parse(txtTotalNightTour.Text);
        //    //_tourInfo.UpdatedBy = UserId;
        //    //_tourInfo.UpdatedDate = DateTime.Now;
        //    //_tourController.UpdateTour(_tourInfo);

        //    //Response.Redirect("/Pages/BangChietTinh.aspx?TourId="+tourInfo.TourId);

        //    _tourInfo.TourId = tourInfo.TourId;
        //    _tourInfo.TourName = tourInfo.TourName;
        //    _tourInfo.TourTypeId = tourInfo.TourTypeId;
        //    _tourInfo.GroupType = tourInfo.GroupType;
        //    _tourInfo.LocationBegin = tourInfo.LocationBegin;
        //    _tourInfo.ScheduleId = tourInfo.ScheduleId;
        //    _tourInfo.QuantityAdult = int.Parse(txtQuantityAdultsTour.Text);
        //    _tourInfo.QuantityChild = int.Parse(txtQuantityChildTour.Text);
        //    _tourInfo.Season = tourInfo.Season;
        //    _tourInfo.MartName = tourInfo.MartName;
        //    _tourInfo.BeginDate =DateTime.Now;
        //    _tourInfo.EndDate = DateTime.Now;
        //    _tourInfo.QuantityDay = tourInfo.QuantityDay;
        //    _tourInfo.QuantityNight = tourInfo.QuantityNight;
        //    _tourInfo.Status = tourInfo.Status;
        //    _tourInfo.UpdatedBy = UserId;
        //    _tourInfo.CreatedDate = tourInfo.CreatedDate;
        //    _tourInfo.UpdatedDate = DateTime.Now;
        //    _tourInfo.CreatedBy = tourInfo.CreatedBy;
        //    _tourController.UpdateTour(_tourInfo);
        //}
        protected void SaveData()
        {
            _tourInfo = _tourController.GetTour(int.Parse(Request.QueryString["TourId"]));


            //_tourInfo.QuantityAdult = int.Parse(txtQuantityAdultsTour.Text);
            //_tourInfo.QuantityChild = int.Parse(txtQuantityChildTour.Text);
            //if (!String.IsNullOrEmpty(dteBeginDateTour.Value))
            //{
            //    var beginDate = Convert.ToDateTime(dteBeginDateTour.Value);
            //    _tourInfo.BeginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, int.Parse(ddlBeginHourTour.SelectedValue), int.Parse(ddlBeginMinuteTour.SelectedValue), 0);
            //}
            //if (!String.IsNullOrEmpty(dteEndDateTour.Value))
            //{
            //    var endDate = Convert.ToDateTime(dteEndDateTour.Value);
            //    _tourInfo.EndDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, int.Parse(ddlEndHourTour.SelectedValue), int.Parse(ddlEndMinuteTour.SelectedValue), 0);
            //}
            //_tourInfo.QuantityDay = int.Parse(txtTotalDayTour.Text);
            //_tourInfo.QuantityNight = int.Parse(txtTotalNightTour.Text);
            //_tourInfo.UpdatedBy = UserId;
            //_tourInfo.UpdatedDate = DateTime.Now;
            //_tourController.UpdateTour(_tourInfo);

            //Response.Redirect("/Pages/BangChietTinh.aspx?TourId="+tourInfo.TourId);

            _tourInfo.TourId = int.Parse(Request.QueryString["TourId"]);
            //_tourInfo.TourName = tourInfo.TourName;
            //_tourInfo.TourTypeId = tourInfo.TourTypeId;
            //_tourInfo.GroupType = tourInfo.GroupType;
            //_tourInfo.LocationBegin = tourInfo.LocationBegin;
            //_tourInfo.ScheduleId = tourInfo.ScheduleId;
            _tourInfo.QuantityAdult = int.Parse(txtQuantityAdultsTour.Text);
            _tourInfo.QuantityChild = int.Parse(txtQuantityChildTour.Text);
            //_tourInfo.Season = tourInfo.Season;
            //_tourInfo.MartName = tourInfo.MartName;
            //_tourInfo.BeginDate = DateTime.Now;
            //_tourInfo.EndDate = DateTime.Now;
            //_tourInfo.QuantityDay = tourInfo.QuantityDay;
            //_tourInfo.QuantityNight = tourInfo.QuantityNight;
            //_tourInfo.Status = tourInfo.Status;
            //_tourInfo.UpdatedBy = UserId;
            //_tourInfo.CreatedDate = tourInfo.CreatedDate;
            //_tourInfo.UpdatedDate = DateTime.Now;
            //_tourInfo.CreatedBy = tourInfo.CreatedBy;
            _tourController.UpdateTour(_tourInfo);

            Response.Redirect("/Pages/BangChietTinh.aspx?TourId=" + int.Parse(Request.QueryString["TourId"]));
        }
        private void LoadTime()
        {
            //for (var i = 0; i < 24; i++)
            //{
            //    //var hour = i < 10 ? "0" + i : i.ToString();
            //    ddlBeginHourTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
            //    ddlEndHourTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
            //}
            //for (var i = 0; i < 60; i++)
            //{
            //    //var minute = i < 10 ? "0" + i : i.ToString();
            //    ddlBeginMinuteTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
            //    ddlEndMinuteTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
            //}
        }
     
        private void LoadFlightArrivals()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/FlightArrivals.xml"));
            var dv = ds.Tables["FlightArrivals"].DefaultView;
            ddlFlightArrivals.DataTextField = "Name";
            ddlFlightArrivals.DataValueField = "ID";
            ddlFlightArrivals.DataSource = dv;
            ddlFlightArrivals.DataBind();
            ddlFlightArrivals.Items.Insert(0, new ListItem("--- Chọn lượt bay ---", "-1"));

            ddlFlightArrivalsStation.DataTextField = "Name";
            ddlFlightArrivalsStation.DataValueField = "ID";
            ddlFlightArrivalsStation.DataSource = dv;
            ddlFlightArrivalsStation.DataBind();
            ddlFlightArrivalsStation.Items.Insert(0, new ListItem("--- Chọn lượt bay ---", "-1"));
        }

        private void LoadSchedule()
        {
            ddlSchedule.DataSource =
                new ScheduleController().ListSchedule()
                    .Where(p => p.Status == (int)Globals.DocumentStatus.Use&&p.ScheduleType==1)
                    .OrderBy(p => p.ScheduleName);

            ddlSchedule.DataTextField = "ScheduleName";
            ddlSchedule.DataValueField = "ScheduleId";
            ddlSchedule.DataBind();

            ddlSchedule.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }

        private void LoadScheduleStation()
        {
            ddlScheduleStation.DataSource =
                new ScheduleController().ListSchedule()
                    .Where(p => p.Status == (int)Globals.DocumentStatus.Use&&p.ScheduleType==2)
                    .OrderBy(p => p.ScheduleName);

            ddlScheduleStation.DataTextField = "ScheduleName";
            ddlScheduleStation.DataValueField = "ScheduleId";
            ddlScheduleStation.DataBind();

            ddlScheduleStation.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }

        private void LoadConcern()
        {
            ddlConcern.DataSource = new ConcernController().ListConcernByStatus((int)Globals.DocumentStatus.Use).Where(p => p.ConcernType == 1).OrderBy(p => p.ConcernFullName);
            ddlConcern.DataTextField = "ConcernFullName";
            ddlConcern.DataValueField = "ConcernId";
            ddlConcern.DataBind();
            
            ddlConcern.Items.Insert(0, new ListItem("--- Chọn hãng---", "-1"));
           
        }


        private void LoadConcernStation()
        {
            ddlConcernStation.DataSource = new ConcernController().ListConcernByStatus((int)Globals.DocumentStatus.Use).Where(p => p.ConcernType == 2).OrderBy(p => p.ConcernFullName);
            ddlConcernStation.DataTextField = "ConcernFullName";
            ddlConcernStation.DataValueField = "ConcernId";
            ddlConcernStation.DataBind();

            ddlConcernStation.Items.Insert(0, new ListItem("--- Chọn hãng---", "-1"));


        }

        


        private void LoadScheduleCar()
        {
            ddlScheduleCar.DataSource =
                new ScheduleController().ListSchedule()
                    .Where(p => p.Status == (int)Globals.DocumentStatus.Use && p.ScheduleType == 4)
                    .OrderBy(p => p.ScheduleName);

            ddlScheduleCar.DataTextField = "ScheduleName";
            ddlScheduleCar.DataValueField = "ScheduleId";
            ddlScheduleCar.DataBind();

            ddlScheduleCar.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }

        private void LoadSoChoCar()
        {
            ddlSoChoCar.Items.Add("4");
            ddlSoChoCar.Items.Add("5");
            ddlSoChoCar.Items.Add("7");
            ddlSoChoCar.Items.Add("8");
            ddlSoChoCar.Items.Add("10");
            ddlSoChoCar.Items.Add("16");
            ddlSoChoCar.Items.Add("30");
            ddlSoChoCar.Items.Insert(0, new ListItem("--- Chọn số chỗ ---", "-1"));
        }
        private void LoadConcernCar()
        {
            ddlConcernCar.DataSource = new ConcernController().ListConcernByStatus((int)Globals.DocumentStatus.Use).Where(p => p.ConcernType == 3).OrderBy(p => p.ConcernFullName);
            ddlConcernCar.DataTextField = "ConcernFullName";
            ddlConcernCar.DataValueField = "ConcernId";
            ddlConcernCar.DataBind();

            ddlConcernCar.Items.Insert(0, new ListItem("--- Chọn hãng---", "-1"));

        }
        private void LoadYearCar()
        {
            int year = DateTime.Now.Year;
            for (int i = 0; i >= -20; i--)
            {
                ddlYearCar.Items.Add((year + i).ToString());
            }
            ddlYearCar.Items.Insert(0, new ListItem("--- Chọn năm ---", "-1"));
        }
     
    
    }
}